package ru.ifmo.rybakov.muclumon.analysis2;

import java.util.Map;

/**
 * Power function implementation
 * @author gleb.rybakov
 */
public class FunctionPower extends Function {

	private String variableName;
	private Double power;
	
	public FunctionPower(String variableName, Double power) {
		this.variableName = variableName;
		this.power = power;
	}
	
	public String toString() {
		if (power == 1.0) {
			return variableName;
		} else {
			return "(" + variableName + "^" + power + ")";
		}
	}

	@SuppressWarnings("unchecked")
	public Double getValue(Map input) {
		Object o = input.get(variableName);
		if (o == null) {
			throw new RuntimeException("No variable found in input for name '" + variableName + "'");
		}
		Double d = (Double) o;
		return Math.pow(d, power);
	}

	public Function[] getFunctions() {
		return new Function[] { this };
	}
}
